<template>
  <el-container style="height: 100%; min-width: 600px">
    <el-header>
      <el-row>
        <el-button type="success"
                   icon="el-icon-plus"
                   @click="centerDialogVisible = true"
                   circle></el-button>
        <el-button type="primary"
                   icon="el-icon-edit"
                   @click="editDialogVisible = true"
                   circle></el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   @click="deleteAll"
                   circle></el-button>
      </el-row>

      <!-- 弹出添加对话框 -->
      <el-dialog title="添加快递"
                 :visible.sync="centerDialogVisible"
                 width="30%"
                 center>
        <!-- 内容 -->
        <el-form label-position='left'
                 label-width="80px"
                 :model="formLabelAlign">
          <el-form-item label="快递编号">
            <el-input v-model="formLabelAlign.courier_id"></el-input>
          </el-form-item>
          <el-form-item label="快递标题">
            <el-input v-model="formLabelAlign.courier_title"></el-input>
          </el-form-item>
          <el-form-item label="收件人">
            <el-input v-model="formLabelAlign.user_id"></el-input>
          </el-form-item>
          <el-form-item label="取件码">
            <el-input v-model="formLabelAlign.courier_code"></el-input>
          </el-form-item>
          <el-form-item label="到达地址（校内）">
            <el-input v-model="formLabelAlign.courier_address"></el-input>
          </el-form-item>
          <!-- 上传文件 -->
          <el-upload class="upload-demo"
                     ref="upload"
                     action="http://121.37.209.61:8888/courier/upload"
                     :data="formLabelAlign"
                     :limit=1
                     :on-preview="handlePreview"
                     :on-remove="handleRemove"
                     :on-success="response => uploadOK(response)"
                     :file-list="fileList"
                     :auto-upload="false">
            <el-button slot="trigger"
                       size="small"
                       type="primary">选取文件</el-button>
            <div slot="tip"
                 class="el-upload__tip">最多只能上传一个文件</div>
          </el-upload>
        </el-form>
        <!-- 内容 -->
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="addCourier">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹出添加对话框 -->

    </el-header>
    <el-main>
      <el-table v-loading="tableLoading"
                type="index"
                align="center"
                :data="tableData"
                stripe
                border
                style="width: 100%"
                height="100%">
        <el-table-column prop="courier_id"
                         label="快递编号"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="user_id"
                         label="收件人"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="courier_title"
                         label="快递标题"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="courier_code"
                         label="取件码"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="courier_address"
                         label="到达地址（校内）"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="courier_img"
                         label="快递图片"
                         width=""
                         align="center">
          <template slot-scope="scope">
            <!-- <img :src="tableData[scope.$index].img" alt="" /> -->
            <img :src="tableData[scope.$index].courier_img"
                 class="img"
                 alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="courier_status"
                         label="快递状态"
                         width=""
                         align="center">
          <template slot-scope="scope">
            <el-switch @change="switchChange(scope.$index, scope.row)"
                       v-model="tableData[scope.$index].courier_status"
                       active-color="#13ce66"
                       inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="danger"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
</template>
<script>
import { HttpGet, HttpPost } from '@/api/http'
export default {
  name: "Courier",
  data () {
    return {
      //上传文件
      fileList: [],
      //表格
      tableData: [],
      tableLoading: true,
      // 弹出添加对话框
      centerDialogVisible: false,
      formLabelAlign: {
        courier_id: '',
        courier_title: '',
        courier_address: '',
        courier_code: '',
        user_id: ''
      }
    };
  },
  created () {
    this.getList()
  },
  methods: {
    addCourier () {
      this.centerDialogVisible = false
      this.submitUpload()
    },
    submitUpload () {
      this.$refs.upload.submit();
    },
    uploadOK (res) {
      if (res.code == 200) {
        this.getList()
        for (let key in this.formLabelAlign) {
          this.formLabelAlign[key] = ''
        }
        this.fileList = []
        this.$message.success('成功,快递导入成功！')
      } else {
        this.$message.error('错误,快递导入失败！');
      }
    },
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePreview (file) {
      console.log(file);
    },
    async switchChange (index, row) {
      let courier_id = row.courier_id
      let courier_status = row.courier_status ? "1" : "0"
      try {
        let res = await HttpGet({
          url: '/courier/updateStatus',
          params: {
            courier_id: courier_id,
            courier_status: courier_status
          }
        })
        if (res.data.code == 200) {
          this.$message.success('成功,修改快递状态成功！')
        } else {
          this.$message.error('错误,修改快递状态失败！');
        }
      }
      catch (error) {
        console.log(error);
        this.$message.error('错误,修改快递状态失败！');
      }
    },
    async handleDelete (index, row) {
      let courier_id = row.courier_id
      try {
        let res = HttpGet({
          url: "/courier/delete",
          params: {
            courier_id: courier_id
          },
        })
        if (res.data.code == 200) {
          this.tableData.splice(index, 1)
          this.$message.success('成功,删除快递成功！')
        } else {
          this.$message.error('错误，删除快递失败！');
        }
      }
      catch (error) {
        console.log(error);
        this.$message.error('错误，删除快递失败！');
      }
    },
    async getList () {
      this.tableLoading = true
      try {
        let res = await HttpGet({ url: "/courier/all" })
        if (res.data.code == 200) {
          this.tableData = res.data.data.map((item) => {
            item.courier_status == 1
              ? (item.courier_status = true)
              : (item.courier_status = false)
            return item
          })
          this.tableLoading = false
        } else {
          this.tableLoading = false
          this.$message.error('加载数据失败')
        }
      } catch (error) {
        console.log(error)
        this.tableLoading = false
        this.$message.error('服务器异常')
      }
    }
  },
};
</script>

<style scoped>
.el-header {
  padding-top: 20px;
  display: flex;
  justify-content: space-between;
}

.el-main {
  min-width: 600px;
  height: 100%;
  overflow: hidden;
}

.img {
  width: 200px;
  height: 100px;
}
</style>

